#include <rtdef.h>
#include "dev_spi.h"
#include <rtdbg.h>
#include <rtthread.h>
#include "RN8306.h"
#include "hc32_ll_clk.h"
#include "cpuHc32f460.h"
#include "hc32_ll_fcg.h"
#include "hc32_ll_adc.h"
#include "hc32_ll_gpio.h"

char u8RxBuffer[1352] = {0};
char u8TxBuffer[128] = {0xFF};

static struct rt_spi_device *spi_dev_RN8306;
static struct rt_spi_device *spi_dev_RN8306DMA;
/*校表参数EMU*/	
const ST_REG_PARA EC_EMU_RegData[] = 
{
        //addr,		    name,                   rst,            set,            ee_addr,    len, mod    
        {0x0100,        "HFConst1",             0x1000,         0x1000,         0x0000,	        2,	3},    //高频脉冲计数寄存器1                     0
        {0x0101,        "HFConst2",             0x1000,         0x1000,         0x0004,	        2,	3},    //相位补偿寄存器1
        {0x0102,        "IStart_PS",            0x0550,         0x03A0,         0x0008,	        2,	1},    //有功视在启动电流阈值寄存器2
        {0x0103,        "IStart_Q",             0x0550,         0x03A0,         0x000C,	        2,	1},    //无功启动电流阈值寄存器3

        {0x0104,        "LostVoltT",            0x0400,         0x0400,         0x0010,	        2,	3},    //失压阈值寄存器4
        {0x0105,        "ZXOT",                 0x0000,         0x0000,         0x0014,	        2,	1},    //过零阈值寄存器5
        {0x0106,        "PRTH1L",               0x0000,         0x00F4,         0x0018,	        2,	1},    //相位分段矫正电流阈值1下限6
        {0x0107,        "PRTH1H",               0x0000,         0x0124,         0x001C,	        2,	1},    //相位分段矫正电流阈值1上限7
        {0x0108,        "PRTH2L",               0x0000,         0x0500,         0x0020,	        2,	1},    //相位分段矫正电流阈值2下限8
        {0x0109,        "PRTH2H",               0x0000,         0x0540,         0x0024,	        2,	1},    //相位分段矫正电流阈值2上限9
        {0x010A,        "IRegion3L",            0x0000,         0x0000,         0x0028,	        2,	3},    //相位分段矫正电流阈值3下限10
        {0x010B,        "IRegion3H",            0x0000,         0x0000,         0x002C,	        2,	3},    //相位分段矫正电流阈值3上限   11    
        {0x010C,        "PHSUA",                0x0000,         0x0000,         0x0030,	        1,	0},    //UA相位校正寄存器12
        {0x010D,        "PHSUB",                0x0000,         0x0000,         0x0034,	        1,	0},    //UB相位校正寄存器13
        {0x010E,        "PHSUC",                0x0000,         0x0000,         0x0038,	        1,	0},    //UC相位校正寄存器14
        {0x010F,        "PHSIA",                0x0000,         0x0000,         0x003C,	        3,	0},    //IA相位校正寄存器15
        {0x0110,        "PHSIB",                0x0000,         0x0000,         0x0040,	        3,	0},    //IB相位校正寄存器16
        {0x0111,        "PHSIC",                0x0000,         0x0000,         0x0044,	        3,	0},    //IC相位校正寄存器17
        {0x0112,        "PHSIN",                0x0000,         0x0000,         0x0048,	        1,	0},    //IN相位校正寄存器   18
        {0x0113,        "GSUA",                 0x0000,         0x0000,         0x004C,	        2,	2},    //UA通道增益19
        {0x0114,        "GSUB",                 0x0000,         0x0000,         0x0050,	        2,	2},    //UB通道增益20
        {0x0115,        "GSUC",                 0x0000,         0x0000,         0x0054,	        2,	2},    //UC通道增益     21
        {0x0116,        "GSIA",                 0x0000,         0x0000,         0x0058,	        2,	2},    //IA通道增益22
        {0x0117,        "GSIB",                 0x0000,         0x0000,         0x005C,	        2,	2},    //IB通道增益23
        {0x0118,        "GSIC",                 0x0000,         0x0000,         0x0060,	        2,	2},    //IC通道增益  24
        {0x0119,        "GSIN",                 0x0000,         0x0000,         0x0064,	        2,	0},    //IN通道增益  25
                
        {0x011A,        "DCOS_UA",              0x0000,         0x0000,         0x0068,	        2,	3},    //UA通道增益直流offset26
        {0x011B,        "DCOS_UB",              0x0000,         0x0000,         0x006C,	        2,	3},    //UB通道增益直流offset27
        {0x011C,        "DCOS_UC",              0x0000,         0x0000,         0x0070,	        2,	3},    //UC通道增益直流offset  28
        {0x011D,        "DCOS_IA",              0x0000,         0x0000,         0x0074,	        2,	3},    //IA通道增益直流offset29
        {0x011E,        "DCOS_IB",              0x0000,         0x0000,         0x0078,	        2,	3},    //IB通道增益直流offset30
        {0x011F,        "DCOS_IC",              0x0000,         0x0000,         0x007C,	        2,	3},    //IC通道增益直流offset 31
        {0x0120,        "DCOS_IN",              0x0000,         0x0000,         0x0080,	        2,	3},    //IN通道增益直流offset  32
                                                                   
        {0x0121,        "UA_OS",                0x0000,         0x0000,         0x0084,	        2,	3},    //UA通道增益offset33
        {0x0122,        "UB_OS",                0x0000,         0x0000,         0x0088,	        2,	3},    //UB通道增益offset34
        {0x0123,        "UC_OS",                0x0000,         0x0000,         0x008C,	        2,	3},    //UC通道增益offset  35
        {0x0124,        "IA_OS",                0x0000,         0x0000,         0x0090,	        2,	2},    //IA通道增益offset36
        {0x0125,        "IB_OS",                0x0000,         0x0000,         0x0094,	        2,	2},    //IB通道增益offset37
        {0x0126,        "IC_OS",                0x0000,         0x0000,         0x0098,	        2,	2},    //IC通道增益offset 38
        {0x0127,        "IN_OS",                0x0000,         0x0000,         0x009C,	        2,	0},    //IN通道增益offset  39
                                                          
        {0x0128,        "GPA",                  0x0000,         0x0000,         0x00A0,	        2,	3},    //A有功功率增益40
        {0x0129,        "GPB",                  0x0000,         0x0000,         0x00A4,	        2,	3},    //B有功功率增益41
        {0x012A,        "GPC",                  0x0000,         0x0000,         0x00A8,	        2,	3},    //C有功功率增益 42
                                                                     
        {0x012B,        "GQA",                  0x0000,         0x0000,         0x00AC,	        2,	3},    //A无功功率增益43
        {0x012C,        "GQB",                  0x0000,         0x0000,         0x00B0,	        2,	3},    //B无功功率增益44
        {0x012D,        "GQC",                  0x0000,         0x0000,         0x00B4,	        2,	3},    //C无功功率增益 45
                                                                    
        {0x012E,        "GSA",                  0x0000,         0x0000,         0x00B8,	        2,	3},    //A视在功率增益46
        {0x012F,        "GSB",                  0x0000,         0x0000,         0x00BC,	        2,	3},    //B视在功率增益47
        {0x0130,        "GSC",                  0x0000,         0x0000,         0x00C0,	        2,	3},    //C视在功率增益   48     
                                                                   
        {0x0131,        "PA_PHSL",              0x0000,         0x0000,         0x00C4,	        2,	2},    //A相有功分段相位校正寄存器低49
        {0x0132,        "PB_PHSL",              0x0000,         0x0000,         0x00C8,	        2,	2},    //B相有功分段相位校正寄存器低50
        {0x0133,        "PC_PHSL",              0x0000,         0x0000,         0x00CC,	        2,	2},    //C相有功分段相位校正寄存器低  51
        {0x0134,        "QA_PHSL",              0x0000,         0x0000,         0x00D0,	        2,	2},    //A相无功分段相位校正寄存器低52
        {0x0135,        "QB_PHSL",              0x0000,         0x0000,         0x00D4,	        2,	2},    //B相无功分段相位校正寄存器低53
        {0x0136,        "QC_PHSL",              0x0000,         0x0000,         0x00D8,	        2,	2},    //C相无功分段相位校正寄存器低  54
        {0x0137,        "PA_OS",                0x0000,         0x0000,         0x00DC,	        2,	3},    //A相有功offset55
        {0x0138,        "PB_OS",                0x0000,         0x0000,         0x00E0,	        2,	3},    //B相有功offset56
        {0x0139,        "PC_OS",                0x0000,         0x0000,         0x00E4,	        2,	3},    //C相有功offset57
        {0x013A,        "QA_OS",                0x0000,         0x0000,         0x00E8,	        2,	3},    //A相无功offset58
        {0x013B,        "QB_OS",                0x0000,         0x0000,         0x00EC,	        2,	3},    //B相无功offset59
        {0x013C,        "QC_OS",                0x0000,         0x0000,         0x00F0,	        2,	3},    //C相无功offset   60    
                                                                     
        {0x013D,        "FUA_OS",               0x0000,         0x0000,         0x00F4,	        2,	3},    //A相基波电压有效值offset61
        {0x013E,        "FUB_OS",               0x0000,         0x0000,         0x00F8,	        2,	3},    //B相基波电压有效值offset62
        {0x013F,        "FUC_OS",               0x0000,         0x0000,         0x00FC,	        2,	3},    //C相基波电压有效值offsett63
        {0x0140,        "FIA_OS",               0x0000,         0x0000,         0x0100,	        2,	3},    //A相基波电流有效值offset64
        {0x0141,        "FIB_OS",               0x0000,         0x0000,         0x0104,	        2,	3},    //B相基波电流有效值offset65
        {0x0142,        "FIC_OS",               0x0000,         0x0000,         0x0108,	        2,	3},    //C相基波电流有效值offset  66   
                                                                     
        {0x0143,        "GFPA",                 0x0000,         0x0000,         0x010C,	        2,	3},    //A相基波有功功率增益67
        {0x0144,        "GFPB",                 0x0000,         0x0000,         0x0110,	        2,	3},    //B相基波有功功率增益68
        {0x0145,        "GFPC",                 0x0000,         0x0000,         0x0114,	        2,	3},    //C相基波有功功率增益69
        {0x0146,        "GFQA",                 0x0000,         0x0000,         0x0118,	        2,	3},    //A相基波无功功率增益70
        {0x0147,        "GFQB",                 0x0000,         0x0000,         0x011C,	        2,	3},    //B相基波无功功率增益71
        {0x0148,        "GFQC",                 0x0000,         0x0000,         0x0120,	        2,	3},    //C相基波无功功率增益    72     
        {0x0149,        "GFSA",                 0x0000,         0x0000,         0x0124,	        2,	3},    //A相基波视在功率增益    73     
        {0x014A,        "GFSB",                 0x0000,         0x0000,         0x0128,	        2,	3},    //B相基波视在功率增益      74   
        {0x014B,        "GFSC",                 0x0000,         0x0000,         0x012C,	        2,	3},    //C相基波视在功率增益       75   
                                                                    
        {0x014C,        "FPA_PHS",              0x0000,         0x0000,         0x0130,	        2,	3},    //A相基波有功相位校正寄存器76
        {0x014D,        "FPB_PHS",              0x0000,         0x0000,         0x0134,	        2,	3},    //B相基波有功相位校正寄存器77
        {0x014E,        "FPC_PHS",              0x0000,         0x0000,         0x0138,	        2,	3},    //C相基波有功相位校正寄存器78
        {0x014F,        "FQA_PHS",              0x0000,         0x0000,         0x013C,	        2,	3},    //A相基波无功相位校正寄存器79
        {0x0150,        "FQB_PHS",              0x0000,         0x0000,         0x0140,	        2,	3},    //B相基波无功相位校正寄存器80
        {0x0151,        "FQC_PHS",              0x0000,         0x0000,         0x0144,	        2,	3},    //C相基波无功相位校正寄存器 81
        {0x0152,        "FPA_OS",               0x0000,         0x0000,         0x0148,	        2,	3},    //A相基波有功offset82
        {0x0153,        "FPB_OS",               0x0000,         0x0000,         0x014C,	        2,	3},    //B相基波有功offset83
        {0x0154,        "FPC_OS",               0x0000,         0x0000,         0x0150,	        2,	3},    //C相基波有功offset84
        {0x0155,        "FQA_OS",               0x0000,         0x0000,         0x0154,	        2,	3},    //A相基波无功offset85
        {0x0156,        "FQB_OS",               0x0000,         0x0000,         0x0158,	        2,	3},    //B相基波无功offset86
        {0x0157,        "FQC_OS",               0x0000,         0x0000,         0x015C,	        2,	3},    //C相基波无功offset87
                                                                     
        {0x0158,        "SAGCFG",               0x0000,         0x0000,         0x0160,	        3,	3},    //电压暂降阈值寄存器88
        {0x0159,        "OVLVL",                0x0000,         0x0000,         0x0164,	        2,	3},    //过压阈值寄存器89
        {0x015A,        "OILVL",                0x0000,         0x0000,         0x0168,	        2,	3},    //过流阈值寄存器 90
        //EMM配置和状态寄存器                                       
        {0x0160,        "CFCFG",                0x000000,       0x077710,       0x016C,	        3,	1},    //CF引脚配置寄存器91
        {0x0161,        "EMMCFG",               0x0000,         0x000000,       0x0170,	        3,	1},    //EMU配置寄存器92
        {0x0162,        "EMUCON",               0x0000,         0x777777,       0x0174,          3,	1},    //EMU控制寄存器93
        {0x0163,        "WSAVECON",             0x0000,         0x0000,         0x0178,         1,	3},    //采样数据写缓存控制寄存器94
        {0x0164,        "EMMIE",                0x0000,         0x0000,         0x017C,         2,	3},    //EMM中断允许寄存器，写保护95
        {0x0165,        "EMMIF",                0x0000,         0x0000,         0x0180,         2,	3},    //EMM中断标志和状态寄存器96
        {0x0166,        "PQSign",               0x0000,         0x0000,         0x0184,         2,	3},    //有功无功功率方向寄存器97
        {0x0167,        "Noload",               0x0000,         0x0000,         0x0188,         2,	3},    //潜动启动状态寄存器98
        {0x0168,        "IRegionS",             0x0000,         0x0000,         0x018C,         1,	3},    //电流分区状态寄存器99
        {0x0169,        "PHASES",               0x0000,         0x0000,         0x0190,         2,	3},    //相电压电流状态寄存器100       
        {0x016A,        "CheckSum1",            0x0000,         0x0000,         0x0194,         3,	3},    //EMM校表和配置寄存器校验和101
        {0x016B,        "EMMIE2",               0x0000,         0x0000,         0x0198,         4,	3},    //EMM中断允许寄存器102
        {0x016C,        "EMMIF2",               0x0000,         0x0000,         0x019C,         4,	3},    //EMM中断标志和状态寄存器103
        {0x016D,        "EMMCFG3",              0x0000,         0x00200008,     0x01A0,         4,	1},    //EMM配置寄存器3     104
        //NVM1配置和状态寄存器                                       
        {0x0170,        "NVM1CFG",              0x0000,         0x0000,         0x01A4,         1,	3},    //NVM1配置寄存器105
        {0x0171,        "EMMCFG3",              0x0000,         0x0000,         0x01A8,         1,	3},    //NVM1状态寄存器   106 
         //NVM2配置和状态寄存器                                       
        {0x0172,        "NVM2CFG",              0x0000,         0x0000,         0x01AC,         2,	3},    //NVM2配置寄存器107
        {0x0173,        "NVM2CMPA",             0x0000,         0x0000,         0x01B0,         2,	3},    //NVM2 IA比较控制寄存器108
        {0x0174,        "NVM2CMPB",             0x0000,         0x0000,         0x01B4,         2,	3},    //NVM2 IB比较控制寄存器109
        {0x0175,        "NVM2CMPC",             0x0000,         0x0000,         0x01B8,         2,	3},    //NVM2 IC比较控制寄存器110
        {0x0176,        "NVM2IF",               0x0000,         0x0000,         0x01BC,         1,	3},    //全失压状态寄存器111
        //系统配置寄存器                                              
        {0x0178,        "SYSCFG",               0x0000,         0x0000,         0x01C0,         4,	3},    //系统配置寄存器112
        {0x017F,        "WREN2",                0x0000,         0x0000,         0x01C4,         4,	3},    //写使能寄存器2113
        {0x0180,        "WREN",                 0x0000,         0x0000,         0x01C8,         1,	3},    //写使能寄存器114
        {0x0181,        "WMSW",                 0x0000,         0x0000,         0x01CC,         1,	3},    //工作模式切换寄存器115
        {0x0182,        "SOFTRST",              0x0000,         0x0000,         0x01D0,         1,	3},    //软件复位寄存器116
        {0x0183,        "ADCCFG",               0x0000,         0x0000,         0x01D4,         2,	3},    //ADC配置寄存器117
        {0x0186,        "MODSEL",               0x0000,         0x0033,         0x01D8,         1,	3},    //三相四线/三相三线模式选择寄存器118
        //EMM校表寄存器                                             
        {0x0190,        "ZXOTU",                0x0000,         0x0000,         0x01DC,         2,	3},    //电压夹角以及测频阈值寄存器119
        {0x0191,        "AUTOCD_EN",            0x0000,         0x0000,         0x01E0,         2,	3},    //直流OFFSET自动校正使能寄存器120
        {0x0192,        "ZXOTCFG",              0x0000,         0x0000,         0x01E4,         2,	3},    //过零计算配置及标志寄存器121
        {0x0193,        "DMA_WAVECON",          0x0000,         0x0000,         0x01E8,         4,	3},    //同步采样通道配置寄存器122
        {0x01B0,        "PA_PHSM",              0x0000,         0x0000,         0x01EC,         3,	2},    //A相有功分段相位校正寄存器中123
        {0x01B1,        "PA_PHSH",              0x0000,         0x0000,         0x01F0,         3,	2},    //A相有功分段相位校正寄存器高   124  
        {0x01B2,        "PB_PHSM",              0x0000,         0x0000,         0x01F4,         3,	2},    //B相有功分段相位校正寄存器中125
        {0x01B3,        "PB_PHSH",              0x0000,         0x0000,         0x01F8,         3,	2},    //B相有功分段相位校正寄存器高  126
        {0x01B4,        "PC_PHSM",              0x0000,         0x0000,         0x01FC,         3,	2},    //C相有功分段相位校正寄存器中127
        {0x01B5,        "PC_PHSH",              0x0000,         0x0000,         0x0200,         3,	2},    //C相有功分段相位校正寄存器高  128
                                                                    
        {0x01B6,        "QA_PHSM",              0x0000,         0x0000,         0x0204,         3,	2},    //A相无功分段相位校正寄存器中129
        {0x01B7,        "QA_PHSH",              0x0000,         0x0000,         0x0208,         3,	2},    //A相无功分段相位校正寄存器高   130  
        {0x01B8,        "QB_PHSM",              0x0000,         0x0000,         0x020C,         3,	2},    //B相无功分段相位校正寄存器中131
        {0x01B9,        "QB_PHSH",              0x0000,         0x0000,         0x0210,         3,	2},    //B相无功分段相位校正寄存器高  132
        {0x01BA,        "QC_PHSM",              0x0000,         0x0000,         0x0214,         3,	2},    //C相无功分段相位校正寄存器中133
        {0x01BB,        "QC_PHSH",              0x0000,         0x0000,         0x0218,         3,	2},    //C相无功分段相位校正寄存器高  134
        
        {0x01E5,        "WAVECON3",             0x0000,         0x0000,         0x021C,         4,	2},    //波形采样通道配置寄存器 3  137
        {0x01C9,        "HSDC_CTL",             0x0000,         0x0000,         0x0220,         4,	2},    //波形采样通道配置寄存器 3  138
}; 



/* SPI配置结构体 */
static struct rt_spi_configuration spi_cfg = {
    .mode = RT_SPI_MODE_1 | RT_SPI_MSB | RT_SPI_MASTER ,
    .data_width = 8,
    .max_hz = 2 * 1000 * 1000,  // 2MHz
};

/* SPIdma配置结构体 */
static struct rt_spi_configuration spi_dma_cfg = {
    .mode = RT_SPI_MODE_1 | RT_SPI_MSB | RT_SPI_SLAVE  ,
    .data_width = 8,
    .max_hz =4 * 1000 * 1000,  // 4MHz
};

/* SPI2硬件初始化 */
static void hc32_spi2_gpio_init(void)
{
    stc_gpio_init_t gpio_init = {
        .u16PinState        = PIN_STAT_RST,
        .u16PinDir          = PIN_DIR_OUT,
        .u16PinOutputType   = PIN_OUT_TYPE_CMOS,
        .u16PinDrv          = PIN_MID_DRV,     
        .u16Latch           = PIN_LATCH_OFF,        
        .u16PullUp          = PIN_PU_OFF,       
        .u16Invert          = PIN_INVT_OFF,  
        .u16ExtInt          = PIN_EXTINT_OFF,       
        .u16PinAttr         = PIN_ATTR_DIGITAL,      
    };
    LL_PERIPH_WE(LL_PERIPH_GPIO);
    // 配置SCK, MOSI, CS为推挽输出
    GPIO_Init(SPI2_SCK_PIN, &gpio_init);
    GPIO_Init(SPI2_MOSI_PIN, &gpio_init);
    GPIO_Init(SPI2_CS_PORT_PIN, &gpio_init);
    
    // 配置MISO为输入
    gpio_init.u16PinDir = PIN_DIR_IN;
    GPIO_Init(SPI2_MISO_PIN, &gpio_init);
    
    // 设置引脚复用功能
    GPIO_SetFunc(SPI2_SCK_PIN, Func_Spi2_Sck);  // SPI1_SCK
    GPIO_SetFunc(SPI2_MISO_PIN, Func_Spi2_Miso); // SPI1_MISO
    GPIO_SetFunc(SPI2_MOSI_PIN, Func_Spi2_Mosi); // SPI1_MOSI
    
    // 初始时拉高片选
    GPIO_ResetPins(SPI2_CS_PORT_PIN);
    
    LL_PERIPH_WP(LL_PERIPH_GPIO);
}

/* SPI2硬件初始化 */
static void hc32_spi3_gpio_init(void)
{
    stc_gpio_init_t gpio_init = {
        .u16PinState        = PIN_STAT_RST,
        .u16PinDir          = PIN_DIR_OUT,
        .u16PinOutputType   = PIN_OUT_TYPE_CMOS,
        .u16PinDrv          = PIN_MID_DRV,     
        .u16Latch           = PIN_LATCH_OFF,        
        .u16PullUp          = PIN_PU_OFF,       
        .u16Invert          = PIN_INVT_OFF,  
        .u16ExtInt          = PIN_EXTINT_OFF,       
        .u16PinAttr         = PIN_ATTR_DIGITAL,      
    };
    
    stc_gpio_init_t gpio_IN_init = {
        .u16PinState        = PIN_STAT_RST,
        .u16PinDir          = PIN_DIR_IN,
        .u16PinOutputType   = PIN_OUT_TYPE_NMOS,
        .u16PinDrv          = PIN_MID_DRV,     
        .u16Latch           = PIN_LATCH_OFF,        
        .u16PullUp          = PIN_PU_OFF,       
        .u16Invert          = PIN_INVT_OFF,  
        .u16ExtInt          = PIN_EXTINT_OFF,       
        .u16PinAttr         = PIN_ATTR_DIGITAL,      
    };
    
    LL_PERIPH_WE(LL_PERIPH_GPIO);
    // 配置SCK, MOSI, CS为推挽输出
    GPIO_Init(SPI3_SCK_PIN, &gpio_init);
    GPIO_Init(SPI3_MOSI_PIN, &gpio_init);
    GPIO_Init(SPI3_CS_PORT_PIN, &gpio_IN_init);
    
    // 设置引脚复用功能
    GPIO_SetFunc(SPI3_SCK_PIN, Func_Spi3_Sck);  // SPI1_SCK
    GPIO_SetFunc(SPI3_MOSI_PIN, Func_Spi3_Mosi); // SPI1_MOSI
    
    LL_PERIPH_WP(LL_PERIPH_GPIO);
}

static void RN8306_Write(uint16_t wReg, uint8_t *pBuf, uint32_t DatLen)
{
    struct rt_spi_message msg[3];
    struct rt_spi_device *spi_dev;
    rt_uint8_t cmd[100] = {0}; // 读ID命令
    rt_uint8_t wren_En[4] = {0x80, 0x90, 0xE5, 0x0A}; //使能
    rt_uint8_t wren_Dis[4] = {0x80, 0x90, 0xDC, 0x13}; //保护
    rt_uint8_t chksum =0, temp=0, i=0;
    rt_uint8_t Revbuf[100] = {0};
    cmd[0] = wReg &0x00FF;
    chksum +=cmd[0];
    cmd[1] = ((wReg>>4) &0x00F0) +0x80;
    chksum +=cmd[1];
    for(i = DatLen; i > 0 ;i-- )
    {
        cmd[2+DatLen-i] = pBuf[i-1];
        chksum +=pBuf[i-1];
    }
    cmd[DatLen+2] = ~chksum;
    spi_dev = (struct rt_spi_device *)rt_device_find(RN8306_SPI_DEVICE_NAME);

    //使能
    msg[0].send_buf   = wren_En;
    msg[0].recv_buf   = RT_NULL;
    msg[0].length     = 4;
    msg[0].cs_take    = 1;
    msg[0].cs_release = 0;
    msg[0].next       = &msg[1];
    
    //地址+数据+校验
    msg[1].send_buf   = cmd;
    msg[1].recv_buf   = RT_NULL;
    msg[1].length     = DatLen+3;
    msg[1].cs_take    = 1;
    msg[1].cs_release = 0;
    msg[1].next       = &msg[2];
    
    //保护
    msg[2].send_buf   = wren_Dis;
    msg[2].recv_buf   = RT_NULL;
    msg[2].length     = 4;
    msg[2].cs_take    = 0;
    msg[2].cs_release = 1;
    msg[2].next       = RT_NULL;
    rt_spi_transfer_message(spi_dev, msg);

}

static void RN8306_Read(uint16_t wReg, uint8_t *pBuf, uint32_t DatLen)
{
    struct rt_spi_message msg[2];
    struct rt_spi_device *spi_dev;
    rt_uint8_t cmd[100] = {0}; // 读ID命令
    rt_uint8_t Revbuf[100] = {0};

    cmd[0] = wReg &0x00FF;
    cmd[1] = (wReg>>4) &0x00F0;
    
    spi_dev = (struct rt_spi_device *)rt_device_find(RN8306_SPI_DEVICE_NAME);

    msg[0].send_buf   = cmd;
    msg[0].recv_buf   = RT_NULL;
    msg[0].length     = 2;
    msg[0].cs_take    = 1;
    msg[0].cs_release = 0;
    msg[0].next       = &msg[1];
        
        
    msg[1].send_buf   = RT_NULL;
    msg[1].recv_buf   = Revbuf;
    msg[1].length     = DatLen+1;
    msg[1].cs_take    = 0;
    msg[1].cs_release = 1;
    msg[1].next       = RT_NULL;
    
    //传输数据
    rt_spi_transfer_message(spi_dev, msg);
    memcpy(pBuf, Revbuf, DatLen);

}

static void RN8306_Wave_Reset()
{
    uint32_t tmp = 0;
    
    RN8306_Write(0X1E5,(uint8_t*)&tmp ,4);   //DMA-WAVECON3
    
    tmp = 0xE1;
    RN8306_Write(0x182, (uint8_t*)&tmp, 1);  //复位 MMU缓存波形

    tmp = 0x4C;
    RN8306_Write(0x182, (uint8_t*)&tmp, 1);  // 复位 HSDC；

}

void RN8306_Wave_Init()
{
    uint32_t data = 0;
    data = (0xE5<<8) | 0x55;
    RN8306_Write(0x17F, (uint8_t*)&data, 4);    

    // 复位
    RN8306_Wave_Reset();
   
    // 数据为1字节， 校验1字节
    RN8306_Read(0x181, (uint8_t*)&data, 1);        

    if((data&0x00FF) == 1)
    {
        //  DMA_WAVECON,  
//            data = ((2048000/50/128) - 1) << 4;
        data = ((2048000/16000) - 1) << 4;
        RN8306_Write(0x193, (uint8_t*)&data, 4);
        
        // DMA_WAVECON2
        data = (7 << 0) | (0 << 3) | (1<<8) | (1<<9) | (0<<10) ;
        RN8306_Write(0x1D1, (uint8_t*)&data, 4);
        
        // DMA_WSAVECON
        data = 1<<7;
        RN8306_Write(0x163, (uint8_t*)&data, 1);
        
        // DMA_HSDC_CTRL
        data = (1<<2) | (0<<1) | 1;
        RN8306_Write(0x1C9, (uint8_t*)&data, 4);
        
        // DMA_WAVECON3
        data = 1;
        RN8306_Write(0x1E5, (uint8_t*)&data, 4);        
    }
    
    data = 0xEA025800;
    RN8306_Write(0x178, (uint8_t*)&data, 4);

    // 关闭写使能
    data = (0xdc << 8)|0x55;
    RN8306_Write(0x17F, (uint8_t*)&data, 4);   
    
//    hc32_spi_dma_trans(spi_config[1], ,u8RxBuffer,1152,);
        
}

static int rt_hw_spi_8306_init(void)
{
    rt_err_t res=0;
    static uint8_t txBuf = 0xff;
    //8306
    FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_SPI2, Enable);
    hc32_spi2_gpio_init();

    LL_PERIPH_WE(LL_PERIPH_GPIO);
    res = rt_hw_spi_device_attach(RN8306_SPI_BUS_NAME, RN8306_SPI_DEVICE_NAME, (void*)SPI2_CS_PPIN);
    LL_PERIPH_WP(LL_PERIPH_GPIO);
    if (res != RT_EOK) {
        rt_kprintf("SPI device attach failed!\n");
        return res;
    }
    ENABLE_FCG0_REG_WRITE();
    spi_dev_RN8306 = (struct rt_spi_device *)rt_device_find(RN8306_SPI_DEVICE_NAME);
    res = rt_spi_configure(spi_dev_RN8306, &spi_cfg);
    DISABLE_FCG0_REG_WRITE();
    
    if (res != RT_EOK) {
        rt_kprintf("SPI config failed!\n");
        return res;
    }
    //DMA
    FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_SPI3, Enable);
    hc32_spi3_gpio_init();
    //DMA为从机，不控制cs
    LL_PERIPH_WE(LL_PERIPH_GPIO);
    res = rt_hw_spi_device_attach(RN8306_DMA_SPI_BUS_NAME, RN8306_DMA_SPI_DEVICE_NAME,  RT_NULL);
    LL_PERIPH_WP(LL_PERIPH_GPIO);
    
    if (res != RT_EOK) {
        rt_kprintf("SPI device attach failed!\n");
        return res;
    }
   
    ENABLE_FCG0_REG_WRITE();
    spi_dev_RN8306DMA = (struct rt_spi_device *)rt_device_find(RN8306_DMA_SPI_DEVICE_NAME);
    res = rt_spi_configure(spi_dev_RN8306DMA, &spi_dma_cfg);
    DISABLE_FCG0_REG_WRITE();
    
    if (res != RT_EOK) {
        rt_kprintf("SPI config failed!\n");
        return res;
    }
    return RT_EOK;
}

/* 导出到自动初始化 */
INIT_COMPONENT_EXPORT(rt_hw_spi_8306_init);

CMD_OPTIONS_STATEMENT(RN8306_Operate)
static int RN8306_Operate(int argc, char **argv)
{
    uint16_t Send_Reg=0,Reg_Len=0;
    uint32_t Rev_Reg=0;
    int ReadNo=0;
    rt_uint8_t Revbuf[100], RevbufString[100];
    memset(Revbuf, 0, 100);
    memset(RevbufString, 0, 100);
    
    if(strlen(argv[1]) != 4)
    {
        rt_kprintf("error:reg len is not 4\n");
        return RT_ERROR;
    }
    StringToHexGroup((char *)&Rev_Reg, argv[1], 4);
    lib_InverseData((char *)&Rev_Reg, 2);
    if(argc == 2)
    {
        for(ReadNo=0; ReadNo<sizeof(EC_EMU_RegData)/sizeof(ST_REG_PARA); ReadNo++)
        {
            if(Rev_Reg == EC_EMU_RegData[ReadNo].addr)
            {
                Send_Reg = EC_EMU_RegData[ReadNo].addr;
                Reg_Len = EC_EMU_RegData[ReadNo].len;
                break;
            }
        }
        if( ReadNo == sizeof(EC_EMU_RegData)/sizeof(ST_REG_PARA))
        {
            goto _usage;   
        }
    }
    else
    {
        goto _usage;   
    }

    RN8306_Read(Send_Reg, Revbuf, Reg_Len);
    HexToAscii(Revbuf, RevbufString, Reg_Len);
    //打印ID
    rt_kprintf("当前 %-12s 为: 0x%-12s\n", EC_EMU_RegData[ReadNo].name, &RevbufString);
    return RT_EOK;
    
_usage:
    rt_kprintf("Usage: rn8306 [options]\n");
    rt_kprintf("[options]:\n");
    for(int i=0;i<sizeof(EC_EMU_RegData)/sizeof(ST_REG_PARA); i++)
    {
       rt_kprintf(" 0%02X  -获取 %-12s  \n", EC_EMU_RegData[i].addr, EC_EMU_RegData[i].name ); 
    }

    return RT_EOK;
    
}

CMD_OPTIONS_NODE_START(RN8306_Operate)
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0100,         获取HFConst1   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0101,         获取HFConst2   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0102,         获取IStart_PS  )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0103,         获取IStart_Q   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0104,         获取LostVoltT  )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0105,         获取ZXOT       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0106,         获取PRTH1L     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0107,         获取PRTH1H     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0108,         获取PRTH2L     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0109,         获取PRTH2H     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       010A,         获取IRegion3L  )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       010B,         获取IRegion3H  )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       010C,         获取PHSUA      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       010D,         获取PHSUB      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       010E,         获取PHSUC      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       010F,         获取PHSIA      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0110,         获取PHSIB      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0111,         获取PHSIC      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0112,         获取PHSIN      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0113,         获取GSUA       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0114,         获取GSUB       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0115,         获取GSUC       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0116,         获取GSIA       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0117,         获取GSIB       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0118,         获取GSIC       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0119,         获取GSIN       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       011A,         获取DCOS_UA    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       011B,         获取DCOS_UB    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       011C,         获取DCOS_UC    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       011D,         获取DCOS_IA    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       011E,         获取DCOS_IB    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       011F,         获取DCOS_IC    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0120,         获取DCOS_IN    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0121,         获取UA_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0122,         获取UB_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0123,         获取UC_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0124,         获取IA_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0125,         获取IB_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0126,         获取IC_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0127,         获取IN_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0128,         获取GPA        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0129,         获取GPB        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       012A,         获取GPC        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       012B,         获取GQA        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       012C,         获取GQB        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       012D,         获取GQC        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       012E,         获取GSA        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       012F,         获取GSB        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0130,         获取GSC        )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0131,         获取PA_PHSL    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0132,         获取PB_PHSL    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0133,         获取PC_PHSL    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0134,         获取QA_PHSL    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0135,         获取QB_PHSL    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0136,         获取QC_PHSL    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0137,         获取PA_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0138,         获取PB_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0139,         获取PC_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       013A,         获取QA_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       013B,         获取QB_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       013C,         获取QC_OS      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       013D,         获取FUA_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       013E,         获取FUB_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       013F,         获取FUC_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0140,         获取FIA_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0141,         获取FIB_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0142,         获取FIC_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0143,         获取GFPA       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0144,         获取GFPB       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0145,         获取GFPC       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0146,         获取GFQA       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0147,         获取GFQB       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0148,         获取GFQC       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0149,         获取GFSA       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       014A,         获取GFSB       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       014B,         获取GFSC       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       014C,         获取FPA_PHS    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       014D,         获取FPB_PHS    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       014E,         获取FPC_PHS    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       014F,         获取FQA_PHS    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0150,         获取FQB_PHS    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0151,         获取FQC_PHS    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0152,         获取FPA_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0153,         获取FPB_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0154,         获取FPC_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0155,         获取FQA_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0156,         获取FQB_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0157,         获取FQC_OS     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0158,         获取SAGCFG     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0159,         获取OVLVL      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       015A,         获取OILVL      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0160,         获取CFCFG      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0161,         获取EMMCFG     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0162,         获取EMUCON     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0163,         获取WSAVECON   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0164,         获取EMMIE      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0165,         获取EMMIF      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0166,         获取PQSign     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0167,         获取Noload     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0168,         获取IRegionS   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0169,         获取PHASES     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       016A,         获取CheckSum1  )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       016B,         获取EMMIE2     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       016C,         获取EMMIF2     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       016D,         获取EMMCFG3    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0170,         获取NVM1CFG    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0171,         获取EMMCFG3    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0172,         获取NVM2CFG    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0173,         获取NVM2CMPA   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0174,         获取NVM2CMPB   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0175,         获取NVM2CMPC   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0176,         获取NVM2IF     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0178,         获取SYSCFG     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       017F,         获取WREN2      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0180,         获取WREN       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0181,         获取WMSW       )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0182,         获取SOFTRST    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0183,         获取ADCCFG     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0186,         获取MODSEL     )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0190,         获取ZXOTU      )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0191,         获取AUTOCD_EN  )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0192,         获取ZXOTCFG    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0193,         获取DMA_WAVECON)
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B0,         获取PA_PHSM    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B1,         获取PA_PHSH    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B2,         获取PB_PHSM    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B3,         获取PB_PHSH    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B4,         获取PC_PHSM    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B5,         获取PC_PHSH    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B6,         获取QA_PHSM    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B7,         获取QA_PHSH    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B8,         获取QB_PHSM    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01B9,         获取QB_PHSH    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01BA,         获取QC_PHSM    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01BB,         获取QC_PHSH    )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0000,         获取U          )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       0000,         获取I          )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01E5,         获取WAVECON3   )
CMD_OPTIONS_NODE(RT_Object_Class_Thread,       01C9,         获取HSDC_CTL   )
                                                                        
CMD_OPTIONS_NODE_END                                                    
MSH_CMD_EXPORT_ALIAS(RN8306_Operate, rn8306, rn8306 objects, optenable);
//MSH_CMD_EXPORT(RN8306_get_volue, RN8306 Operate);                     
                                                                              
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        